package it.rossignoli.enrico.app.Tesi;

import java.util.List;

import de.dfki.ccaal.gestures.IGestureRecognitionListener;
import de.dfki.ccaal.gestures.IGestureRecognitionService;
import de.dfki.ccaal.gestures.classifier.Distribution;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;

public class ExecTrain extends Activity 
{
	
	
	IGestureRecognitionService recognitionService;
	
	int count=0;
	boolean istrain=false;


	//metodi necessari al servizio gesti
	//creazione della connessione
	private final ServiceConnection serviceConnection = new ServiceConnection() 
	{

		
		public void onServiceConnected(ComponentName className, IBinder service) 
		{
			recognitionService = IGestureRecognitionService.Stub.asInterface(service);
			try 
			{
				//Faccio partire in modalita riconoscimento e attivo pure il recorder
				//recognitionService.startClassificationMode("default");
				//Registro il listener in maniera tale da poter recepire le informazioni inviate dal servizio sfruttando i metodi dell interfaccia listener
				//Posso quindi usare i metodi implementati nello stub di questa activity

				recognitionService.registerListener(IGestureRecognitionListener.Stub.asInterface(gestureListenerStub));
			} 
			catch (RemoteException e1) 
			{
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
		}

		
		public void onServiceDisconnected(ComponentName className) 
		{
			recognitionService = null;
		}
	};

	protected boolean imparato;

	//implementazione dei metodi di ritorno dello stub
	IBinder gestureListenerStub = new IGestureRecognitionListener.Stub() 
	{

		
		public void onGestureLearned(final String gestureName) throws RemoteException 
		{
			
			System.err.println("Gesture"+ gestureName +" learned");
			count++;
			imparato=false;
			if(count!=3)
			{
				runOnUiThread(new Runnable() 
				{
					
					public void run() 
					{
						Toast.makeText(ExecTrain.this, gestureName+":"+count, Toast.LENGTH_LONG).show();
						
					}
				});
			}
			else
			{
				imparato=true;
//				runOnUiThread(new Runnable() 
//				{
//					public void run() 
//					{
//						Toast.makeText(ExecTrain.this, String.format("%s imparato",gestureName), Toast.LENGTH_LONG).show();
//						
//					}
//				});
			}
			stop();
		}

		
		private void stop() 
		{
			// TODO Auto-generated method stub
			try 
			{
				recognitionService.stopLearnMode();
				istrain=false;
				if(count==3)
				{
					count=0;
					//recognitionService.stopLearnMode();
					Toast.makeText(ExecTrain.this, "Gesto Imparato", Toast.LENGTH_LONG).show();
					finish();
				}
			} 
			catch (RemoteException e) 
			{
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}


		public void onTrainingSetDeleted(String trainingSet) throws RemoteException 
		{
//			Toast.makeText(ProvaTrainingActivity.this, String.format("Training set %s deleted", trainingSet), Toast.LENGTH_SHORT).show();
//			System.err.println(String.format("Training set %s deleted", trainingSet));
		}

		
		public void onGestureRecognized(final Distribution distribution) throws RemoteException 
		{
			runOnUiThread(new Runnable() 
			{
				
				public void run() 
				{
//					Toast.makeText(ProvaTrainingActivity.this, String.format("%s: %f", distribution.getBestMatch(), distribution.getBestDistance()), Toast.LENGTH_LONG).show();
//					System.err.println(String.format("%s: %f", distribution.getBestMatch(), distribution.getBestDistance()));
				}
			});
		}
	};
	
	
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) 
	{
		super.onCreate(savedInstanceState);
		Log.i("ActivityDemo", "Richiamato onCreate() con bundle " + savedInstanceState);
		
		setContentView(R.layout.train);
	
		Button BTrain = (Button)findViewById(R.id.BTrain);
		
//		BTrain.setOnTouchListener(new OnTouchListener() 
//        {
//			
//			
//
//			public boolean onTouch(View v, MotionEvent event) 
//			{
//				// TODO Auto-generated method stub
//				
//				if((event.getAction()==MotionEvent.ACTION_DOWN))
//				{
//					try 
//					{
//						
//						SharedPreferences prefs= getSharedPreferences("PREFERENCES_UTENTE", Context.MODE_PRIVATE);
//						String Set=prefs.getString("Selected", "default").toString();
//						String Gesto = getIntent().getExtras().getString("Gesto").toString();
//						recognitionService.startLearnMode(Set, Gesto);
//						istrain=true;
//					} 
//					catch (RemoteException e) 
//					{
//						// TODO Auto-generated catch block
//						e.printStackTrace();
//					}
//				}
//				else if(event.getAction()==MotionEvent.ACTION_UP)
//				{
//					
//					
//				}
//				return false;
//			}
//		});
		
		BTrain.setOnClickListener(new OnClickListener() 
		{
			
			public void onClick(View v) 
			{
				// TODO Auto-generated method stub
				try 
				{
					
					SharedPreferences prefs= getSharedPreferences("PREFERENCES_UTENTE", Context.MODE_PRIVATE);
					String Set=prefs.getString("Selected", "default").toString();
					String Gesto = getIntent().getExtras().getString("Gesto").toString();
					recognitionService.startLearnMode(Set, Gesto);
					istrain=true;
				} 
				catch (RemoteException e) 
				{
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		});
	
	}
	
	@Override
	protected void onRestart() 
	{
		super.onRestart();
		Log.i("ActivityDemo", "Richiamato onRestart()");
	}
	
	@Override
	protected void onStart() 
	{
		super.onStart();
		Log.i("ActivityDemo", "Richiamato onStart()");
	}
	
	@Override
	protected void onResume() 
	{
		Log.i("ActivityDemo", "Richiamato onResume()");
		
		Intent bindIntent = new Intent("de.dfki.ccaal.gestures.GESTURE_RECOGNIZER");
		bindService(bindIntent, serviceConnection, Context.BIND_AUTO_CREATE);
		
		super.onResume();
		
	}
	
	@Override
	protected void onPause() 
	{
		
		Log.i("ActivityDemo", "Richiamato onPause()");
		
		//Disconnetto il listener ai metodi dello stub
		try 
		{
			recognitionService.unregisterListener(IGestureRecognitionListener.Stub.asInterface(gestureListenerStub));
		} catch (RemoteException e) 
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		recognitionService = null;
		//mi disconetto dal servizio
		unbindService(serviceConnection);
		
		super.onPause();
	}
	
	@Override
	protected void onStop() 
	{
		super.onStop();
		Log.i("ActivityDemo", "Richiamato onStop()");
	}
	
	@Override
	protected void onDestroy() 
	{
		super.onDestroy();
		Log.i("ActivityDemo", "Richiamato onDestroy()");
	}
	
	
	
	
	
}